package jlib.component.scroll.skin
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	internal final class Button extends Sprite
	{
		static private const MIN_W:int = 20;
		static private const MIN_H:int = 20;
		
		private var _width:Number = MIN_W;
		private var _height:Number = MIN_H;
		private var _scaleX:Number = 1;
		private var _scaleY:Number = 1;
		
		private var fill:uint;
		
		private var m_center:Shape;
		
		public function Button()
		{
			this.buttonMode = true;
			this.addEventListener(MouseEvent.ROLL_OVER, rollOverState );
			this.addEventListener(MouseEvent.ROLL_OUT, rollOutState );
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownState );
			this.addEventListener(MouseEvent.MOUSE_UP, mouseUpState );
			setDefault();
		}
		
		private function setDefault(): void
		{
			fill = 0xebebeb;
			_width = _height = 0;
			_scaleX = _scaleY = 1;
			redraw();
		}
		
		private function rollOverState(e:Event): void
		{
			fill = 0xffffff;
			redraw();
		}
		
		private function rollOutState(e:Event): void
		{
			fill = 0xebebeb;
			redraw();
		}
		
		private function mouseDownState(e:Event): void
		{
			fill = 0xffffff;
			redraw();
		}
		
		private function mouseUpState(e:Event): void
		{
			fill = 0xebebeb;
			redraw();
		}
		
		private function redraw(): void
		{
			drawBox();
			drawWhiteLine();
		}
		
		private function drawBox(): void
		{
			this.graphics.clear();
			Drawer.box(graphics, fill, 0xc7c7c7, _width, _height);
		}
		
		private function drawWhiteLine(): void
		{
			Drawer.outline(graphics, _width, _height);
		}
		
		public function setCenterShape($shape:Shape): void
		{
			m_center = $shape;
			addChild(m_center);
			m_center.x = _width/2;
			m_center.y = _height/2;
		}
		
		override public function get width(): Number  { return _width; }
		override public function set width(value:Number) : void 
		{
			if(value < MIN_W) value = MIN_W;
			_width = value;
			m_center.x = _width/2;
			redraw();
		}
		
		override public function get height(): Number { return _height; }
		override public function set height(value:Number) : void 
		{
			if(value < MIN_H) value = MIN_H;
			_height = value;
			m_center.y = _height/2;
			redraw();
		}
		
		override public function get scaleX(): Number { return _scaleX; }
		override public function set scaleX(value:Number) : void 
		{
			_scaleX = value;
			redraw();
		}
		
		override public function get scaleY(): Number { return _scaleY; }
		override public function set scaleY(value:Number) : void 
		{
			_scaleY = value;
			redraw();
		}
	}//c
}